<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    let boxes = [1,3,2,2,2,3,4,3,1]; // 通过
    // let boxes =[1,2,1,2,1] ;// 未通过
    let xxxx = 20;
    let removeBoxes = function(boxes) {

        if(boxes.length===1){
            return 1
        }
        let integral = 0;
        let arr = [];
        function setArr(box) {
            let sIndex = 0,eIndex = 0;
            for(let i = 1; i < box.length ; i++){
                // console.log(box[i], box[i - 1],box[i] === box[i - 1],'index',i);
                if(box[i - 1] === box[i]){
                    if(arr[arr.length]) {
                        arr[arr.length].push({item:box[i],index:i})
                    }else {
                        sIndex = i;
                        arr[arr.length] = [{item:box[i],index:i}]
                    }
                }
            }
            eIndex = arr[arr.length-1].length + sIndex;
            let temp = box.splice(sIndex,eIndex);
            console.log(temp,'temp',sIndex,eIndex,arr);
            if(temp.length && xxxx){
                xxxx--;
                setArr(box)
            }
        }
        setArr(boxes);

        console.log(arr);
    };

    removeBoxes(boxes)

</script>
</html>
